/**
 * @author <pidzama@gmail.com> 
 * 
 */
package spwodne;

import java.util.*;
import javax.swing.table.AbstractTableModel;

public class MTSzukaj extends AbstractTableModel {

    MTSzukaj(List l, String imie, String nazwisko) {
        this.l = l;
        lk = szukaj(imie, nazwisko);
        fireTableDataChanged();
    }

    public List szukaj(String i, String n) {
        List llk = new ArrayList();
        for (Iterator i$ = l.iterator(); i$.hasNext();) {
            Spolka s = (Spolka) i$.next();
            Iterator ii$ = s.getLWsi().iterator();
            while (ii$.hasNext()) {
                Wies w = (Wies) ii$.next();
                Iterator iii$ = w.getLKli().iterator();
                while (iii$.hasNext()) {
                    Klient k = (Klient) iii$.next();
                    if (k.getNaz().trim().toLowerCase().matches(n.trim().toLowerCase() + "[\\S]*") && k.getIm().trim().toLowerCase().matches(i.trim().toLowerCase() + "[\\S]*")) {
                        llk.add(k);

                    }
                }
            }
        }
        lk = llk;
        fireTableDataChanged();
        return llk;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return lk.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col) {

        if (lk == null) {
            lk = new ArrayList();
        }
        if (lk.size() == 0) {
            return "";
        }
        Object tmp;
        switch (col) {
            case 0: // '\0'

                tmp = ((Klient) lk.get(row)).getNr();
                break;

            case 1: // '\001'

                tmp = ((Klient) lk.get(row)).getNaz();
                break;

            case 2: // '\002'

                tmp = ((Klient) lk.get(row)).getIm();
                break;

            default:
                System.out.println("getValue out of bounds");
                tmp = null;
                break;
        }
        return tmp;


    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public List getLK() {
        return lk;
    }
    private List l;
    private List lk;
    private String columnNames[] = {
        "ID", "Nazwisko", "Imie"
    };
}
